package com.boulderai.mcp.llm;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 大模型客户端工厂
 * 根据配置创建不同类型的大模型客户端
 */
@Component
public class LlmClientFactory {
    
    @Value("${llm.openai.api-key:}")
    private String openaiApiKey;
    
    @Value("${llm.openai.base-url:https://api.openai.com}")
    private String openaiBaseUrl;
    
    @Value("${llm.claude.api-key:}")
    private String claudeApiKey;
    
    @Value("${llm.claude.base-url:https://api.anthropic.com}")
    private String claudeBaseUrl;
    
    private final Map<String, LlmClient> clientCache = new HashMap<>();
    
    /**
     * 创建OpenAI客户端
     */
    public LlmClient createOpenAiClient() {
        return createOpenAiClient(openaiApiKey, openaiBaseUrl);
    }
    
    /**
     * 创建OpenAI客户端（自定义参数）
     */
    public LlmClient createOpenAiClient(String apiKey, String baseUrl) {
        String cacheKey = "openai_" + apiKey + "_" + baseUrl;
        return clientCache.computeIfAbsent(cacheKey, k -> 
            OpenAiLlmClient.builder()
                .apiKey(apiKey)
                .baseUrl(baseUrl)
                .build());
    }
    
    /**
     * 创建Claude客户端（预留接口）
     */
    public LlmClient createClaudeClient() {
        // TODO: 实现Claude客户端
        throw new UnsupportedOperationException("Claude client not implemented yet");
    }
    
    /**
     * 根据类型创建客户端
     */
    public LlmClient createClient(String clientType) {
        switch (clientType.toLowerCase()) {
            case "openai":
                return createOpenAiClient();
            case "claude":
                return createClaudeClient();
            default:
                throw new IllegalArgumentException("Unsupported client type: " + clientType);
        }
    }
    
    /**
     * 根据类型和自定义参数创建客户端
     */
    public LlmClient createClient(String clientType, Map<String, String> config) {
        switch (clientType.toLowerCase()) {
            case "openai":
                String apiKey = config.getOrDefault("apiKey", openaiApiKey);
                String baseUrl = config.getOrDefault("baseUrl", openaiBaseUrl);
                return createOpenAiClient(apiKey, baseUrl);
            case "claude":
                // TODO: 实现Claude客户端配置
                throw new UnsupportedOperationException("Claude client not implemented yet");
            default:
                throw new IllegalArgumentException("Unsupported client type: " + clientType);
        }
    }
    
    /**
     * 获取支持的客户端类型
     */
    public String[] getSupportedClientTypes() {
        return new String[]{"openai", "claude"};
    }
    
    /**
     * 检查客户端类型是否支持
     */
    public boolean isClientTypeSupported(String clientType) {
        for (String supportedType : getSupportedClientTypes()) {
            if (supportedType.equalsIgnoreCase(clientType)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 清理客户端缓存
     */
    public void clearCache() {
        clientCache.clear();
    }
}